<?php

class Form extends Controller
{
    function From()
    {
        parent::Controller();
        
        $this->load->helper(array('form', 'url'));
    }
    
    function add()
    {
        $this->load->view('admin_view_add', array('table' => $_POST['table']));
    }
    
    function modify($table, $id = NULL)
    {
        if (! is_null($id)):
            $this->load->model('ignitedrecord/ignitedrecord');
            $this->load->model($table);
        
            $record = $this->$table->find($id);
        else:
            $record = NULL;
        endif;
        
        $related = $this->$table->get_related($record->id);
        
        $this->load->view('admin_view_modify', array('table' => $table, 'record' => $record, 'related' => $related));
    }
    
    function submit_modify()
    {
        $this->load->library('form_validation');

        $table = $_POST['table'];
        $id = $_POST['id'];
        
        if ($table == 'teacher' || $table == 'parentt' || $table == 'student'):
            $this->form_validation->set_rules('firstname', 'Imię', 'trim|required|alpha');
            $this->form_validation->set_rules('secondname', 'Nazwisko', 'trim|required|alpha');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        endif;
        
        switch($table):
            case 'subject':
                $this->form_validation->set_rules('name', 'Nazwa przedmiotu', 'trim|required|alpha');    
                break;
            case 'classe':
                $this->form_validation->set_rules('name', 'Nazwa klasy', 'trim|required|alpha_numeric');    
                break;
            case 'teacher':
                $this->form_validation->set_message('teacher_subject_check', 'Nie wybrano przedmiotu uczonego przez nauczyciela');
                $this->form_validation->set_message('teacher_classes_check', 'Nie wybrano klas w których nauczyciel uczy przedmiotu');
                
                $this->form_validation->set_rules('telephone', 'Numer telefonu', 'trim|required|numeric');
                $this->form_validation->set_rules('classe_id', '', 'callback_always_true');
                $this->form_validation->set_rules('classe', '', 'callback_always_true');
                $this->form_validation->set_rules('subject_id', 'Przedmioty', 'trim|callback_teacher_subject_check');
                $this->form_validation->set_rules('subject', 'Przedmioty', 'callback_always_true');
                $this->form_validation->set_rules('classes_id', '', 'callback_teacher_classes_check');
                $this->form_validation->set_rules('classes', '', 'callback_always_true');
                break;
            case 'parentt':
                $this->form_validation->set_message('parentt_student_check', 'Każdy rodzic musi mieć przypisane przynajmniej jedno dziecko');
                
                $this->form_validation->set_rules('telephone', 'Numer telefonu', 'trim|required|numeric');
                $this->form_validation->set_rules('address', 'Adres', 'required');
                $this->form_validation->set_rules('student_id', '', 'callback_parentt_student_check');
                $this->form_validation->set_rules('student', '', 'callback_always_true');
                break;
            case 'student':
                $this->form_validation->set_message('student_classe_check', 'Uczeń musi być przypisany do klasy');
                $this->form_validation->set_message('date_check', 'Nieprawidłowa wartość w polu %s');
                
                $this->form_validation->set_rules('birth', 'Data urodzenia', 'trim|exact_length[10]|callback_date_check');
                $this->form_validation->set_rules('classe_id', '', 'callback_student_classe_check');
                $this->form_validation->set_rules('classe', '', 'callback_always_true');
                break;
        endswitch;
            
        if ($this->form_validation->run() == FALSE):
			$this->load->view('admin_view_modify', array('table' => $table, 'record' => NULL, 'id' => $id));
		else:
            $this->load->model('ignitedrecord/ignitedrecord');
            $this->load->model($table);
            $this->$table->update($_POST, $id);
            
            $this->load->view('admin_view_formsuccess', array('table' => $table));
        endif;
    }
    
    function submit_add()
    {
		$this->load->library('form_validation');

        $table = $_POST['table'];
			
		if ($table == 'teacher' || $table == 'parentt' || $table == 'student'):
            $this->form_validation->set_message('login_available_check', 'Wybrany przez Ciebie Login jest niedostępny');
            
            $this->form_validation->set_rules('firstname', 'Imię', 'trim|required|alpha');
            $this->form_validation->set_rules('secondname', 'Nazwisko', 'trim|required|alpha');
            $this->form_validation->set_rules('login', 'Login', 'trim|required|min_length[5]|max_length[12]|alpha_numeric|callback_login_available_check');
            $this->form_validation->set_rules('passwd', 'Hasło', 'trim|required|min_length[8]|alpha_numeric|matches[passwd2]');
            $this->form_validation->set_rules('passwd2', 'Powtórz hasło', 'trim|required');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        endif;
        
        switch($table):
            case 'subject':
                $this->form_validation->set_rules('name', 'Nazwa przedmiotu', 'trim|required|alpha');    
                break;
            case 'classe':
                $this->form_validation->set_rules('name', 'Nazwa klasy', 'trim|required|alpha_numeric');    
                break;
            case 'teacher':
                $this->form_validation->set_message('teacher_subject_check', 'Nie wybrano przedmiotu uczonego przez nauczyciela');
                $this->form_validation->set_message('teacher_classes_check', 'Nie wybrano klas w których nauczyciel uczy przedmiotu');
                
                $this->form_validation->set_rules('telephone', 'Numer telefonu', 'trim|required|numeric');
                $this->form_validation->set_rules('classe_id', '', 'callback_always_true');
                $this->form_validation->set_rules('classe', '', 'callback_always_true');
                $this->form_validation->set_rules('subject_id', 'Przedmioty', 'trim|callback_teacher_subject_check');
                $this->form_validation->set_rules('subject', 'Przedmioty', 'callback_always_true');
                $this->form_validation->set_rules('classes_id', '', 'callback_teacher_classes_check');
                $this->form_validation->set_rules('classes', '', 'callback_always_true');
                
                
                break;
            case 'parentt':
                $this->form_validation->set_message('parentt_student_check', 'Każdy rodzic musi mieć przypisane przynajmniej jedno dziecko');
                
                $this->form_validation->set_rules('telephone', 'Numer telefonu', 'trim|required|numeric');
                $this->form_validation->set_rules('address', 'Adres', 'required');
                $this->form_validation->set_rules('student_id', '', 'callback_parentt_student_check');
                $this->form_validation->set_rules('student', '', 'callback_always_true');
                break;
            case 'student':
                $this->form_validation->set_message('student_classe_check', 'Uczeń musi być przypisany do klasy');
                $this->form_validation->set_message('date_check', 'Nieprawidłowa wartość w polu %s');
                
                $this->form_validation->set_rules('birth', 'Data urodzenia', 'trim|exact_length[10]|callback_date_check');
                $this->form_validation->set_rules('classe_id', '', 'callback_student_classe_check');
                $this->form_validation->set_rules('classe', '', 'callback_always_true');
                break;
        endswitch;
			
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin_view_add', array('table' => $_POST['table']));
		}
		else
		{
            $this->load->model('ignitedrecord/ignitedrecord');
            $this->load->model($table);
                     
            $rec = $this->$table->new_record($_POST);
                                
            if ($table == 'teacher' || $table == 'student' || $table == 'parentt'):
                $this->load->model('login');
                $log = $this->login->new_record($_POST);
                if ( ! $rec->add_relationship($log)):
                    //echo 'Błąd przy dodawaniu rekordu powiązanego (login)';
                endif;
            endif;
            
            if ($table == 'teacher'):            
                $this->load->model('master');
                $this->load->model('subject');
                $this->load->model('classe');
                if ($_POST['classe_id'] != ""):
                    $mas = $this->master->new_record($_POST);
                    if ( ! $rec->add_relationship($mas)):
                        //echo 'Błąd przy dodawaniu rekordu powiązanego (wychowawca)';
                    endif;
                endif;
                
                $rec->save();
                
                $this->db->select_max('id');
                $q = $this->db->get('teachers');
                foreach ($q->result() as $r):
                    foreach (explode(';', $_POST['classes_id']) as $clid):
                        if (strlen($clid) > 0):
                            $this->db->insert('subjects_teachers', array(   'subject_id' => $_POST['subject_id'],
                                                                            'teacher_id' => $r->id,
                                                                            'classe_id' => $clid));
                        endif;
                    endforeach;
                endforeach;
                
                $this->load->view('admin_view_formsuccess', array('table' => $table));
                return;
            endif;
            
            if ($table == 'student'):
                $this->load->model('classe');
                
                $cl = $this->classe->find($_POST['classe_id']);
                if ( ! $cl->add_relationship($rec)):
                    //echo 'Błąd przy dodawaniu rekordu powiązanego (klasa)';
                endif;            
            endif;
            
            if ($table == 'parentt'):
                $this->load->model('student');
                
                foreach (explode(';', $_POST['student_id']) as $stid):
                    if (strlen($stid) > 0):
                        $ch = $this->student->find($stid);
                        $rec->add_relationship($ch);
                    endif;
                endforeach;
            endif;
        
            if (! $rec->save()):
                //echo 'Błąd przy dodawaniu rekordu';
            endif;
            
            $this->load->view('admin_view_formsuccess', array('table' => $table));
		}
        
        
    }

    function teacher_subject_check($str)
    {
        return (strlen($str) == 0 ? FALSE : TRUE);
    }
    
    function parentt_student_check($str)
    {
        return (strlen($str) == 0 ? FALSE : TRUE);
    }
    
    function student_classe_check($str)
    {
        return (strlen($str) == 0 ? FALSE : TRUE);
    }
    
    function teacher_classes_check($str)
    {
        return (strlen($str) == 0 ? FALSE : TRUE);
    }
    
    function always_true($str)
    {
        return TRUE;
    }
    
    function date_check($str)
    {
        $ar = explode("-", $str);
        
        if (count($ar) != 3):
            return FALSE;
        endif;
        
        if ((strlen($ar[0]) != 4) || (strlen($ar[1]) != 2) || (strlen($ar[2]) != 2)):
            return FALSE;
        endif;
        
        $today = getdate();
        $numeric = array();
        foreach ($ar as $el):
            $numeric[] = intval($el);
        endforeach;
        
        if (($numeric[0] < 1900) || ($numeric[0] > $today['year']) ||
            ($numeric[1] < 1) || ($numeric[1] > 12) ||
            ($numeric[2] < 1) || ($numeric[2] > 31)):
            return FALSE;
        endif;
        
        return TRUE;
    }
    
    function login_available_check($str)
    {
        $this->load->model('ignitedrecord/ignitedrecord');
        $this->load->model('login');
        return $this->login->login_available($str);
    }
}

?>
